package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.Building;
import com.yugong.entity.dto.Room;
import com.yugong.entity.vo.request.BuildingRequestVO;
import com.yugong.entity.vo.response.BuildingListResponseVO;
import com.yugong.mapper.BuildingMapper;
import com.yugong.mapper.RoomMapper;
import com.yugong.service.BuildingService;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 楼栋业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/18
 */
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {
    @Resource
    private BuildingMapper buildingMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private TimeUtils timeUtils;

    /**
     * 根据楼栋名查询楼栋
     *
     * @param pageNum      当前页
     * @param pageSize     当前页数据总个数
     * @param buildingName 楼栋名
     * @return 楼栋
     */
    @Override
    public PageInfo<BuildingListResponseVO> findBuilding(Integer pageNum, Integer pageSize, String buildingName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Building> buildingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        buildingLambdaQueryWrapper
                .like(Building::getBuildingName, buildingName)
                .orderByDesc(Building::getBuildingCreateTime);
        List<Building> buildingList = buildingMapper.selectList(buildingLambdaQueryWrapper);
        QueryWrapper<Building> buildingQueryWrapper = new QueryWrapper<>();
        buildingQueryWrapper.like("building_name", buildingName);
        Long buildingCountNumber = buildingMapper.selectCount(buildingQueryWrapper);
        List<BuildingListResponseVO> buildingListResponseVOList = new ArrayList<>();
        for (Building building : buildingList) {
            BuildingListResponseVO buildingListResponseVO = new BuildingListResponseVO();
            buildingListResponseVO.setBuildingNo(building.getBuildingNo());
            buildingListResponseVO.setBuildingName(building.getBuildingName());
            buildingListResponseVO.setBuildingCreateMan(building.getBuildingCreateMan());
            String createTime = timeUtils.setDateToStrDate(building.getBuildingCreateTime(), Const.YYYY_MM_DD);
            buildingListResponseVO.setBuildingCreateTime(createTime);
            if (building.getBuildingUpdateMan() == null) {
                buildingListResponseVO.setBuildingUpdateMan("未更新");
            } else {
                buildingListResponseVO.setBuildingUpdateMan(building.getBuildingUpdateMan());
            }
            if (building.getBuildingUpdateTime() == null) {
                buildingListResponseVO.setBuildingUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(building.getBuildingUpdateTime(), Const.YYYY_MM_DD);
                buildingListResponseVO.setBuildingUpdateTime(updateTime);
            }
            buildingListResponseVO.setBuildingCountNumber(buildingCountNumber);
            buildingListResponseVOList.add(buildingListResponseVO);
        }
        return PageInfo.of(buildingListResponseVOList);
    }

    /**
     * 查询楼栋
     *
     * @return 楼栋
     */
    @Override
    public List<BuildingListResponseVO> findBuilding() {
        LambdaQueryWrapper<Building> buildingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Building> buildingList = buildingMapper.selectList(buildingLambdaQueryWrapper);
        List<BuildingListResponseVO> buildingListResponseVOList = new ArrayList<>();
        for (Building building : buildingList) {
            BuildingListResponseVO buildingListResponseVO = new BuildingListResponseVO();
            buildingListResponseVO.setBuildingNo(building.getBuildingNo());
            buildingListResponseVO.setBuildingName(building.getBuildingName());
            buildingListResponseVO.setBuildingCreateMan(building.getBuildingCreateMan());
            String createTime = timeUtils.setDateToStrDate(building.getBuildingCreateTime(), Const.YYYY_MM_DD);
            buildingListResponseVO.setBuildingCreateTime(createTime);
            if (building.getBuildingUpdateMan() == null) {
                buildingListResponseVO.setBuildingUpdateMan("未更新");
            } else {
                buildingListResponseVO.setBuildingUpdateMan(building.getBuildingUpdateMan());
            }
            if (building.getBuildingUpdateTime() == null) {
                buildingListResponseVO.setBuildingUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(building.getBuildingUpdateTime(), Const.YYYY_MM_DD);
                buildingListResponseVO.setBuildingUpdateTime(updateTime);
            }
            buildingListResponseVOList.add(buildingListResponseVO);
        }
        return buildingListResponseVOList;
    }

    /**
     * 新增楼栋
     *
     * @param buildingRequestVO 楼栋请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int addBuilding(BuildingRequestVO buildingRequestVO) {
        Building building = new Building();
        building.setBuildingNo(buildingRequestVO.getBuildingNo());
        building.setBuildingName(buildingRequestVO.getBuildingName());
        building.setBuildingCreateMan(buildingRequestVO.getBuildingCreateMan());
        building.setBuildingCreateTime(new Date());
        return buildingMapper.insert(building);
    }

    /**
     * 更新楼栋
     *
     * @param buildingRequestVO 楼栋请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateBuilding(BuildingRequestVO buildingRequestVO) {
        Building building = new Building();
        building.setBuildingNo(buildingRequestVO.getBuildingNo());
        building.setBuildingName(buildingRequestVO.getBuildingName());
        building.setBuildingUpdateMan(buildingRequestVO.getBuildingUpdateMan());
        building.setBuildingUpdateTime(new Date());
        return buildingMapper.updateById(building);
    }

    /**
     * 删除楼栋
     *
     * @param buildingNo 楼栋号
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteBuilding(String buildingNo) {
        LambdaQueryWrapper<Room> roomLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLambdaQueryWrapper.eq(Room::getRoomBuildingNo, buildingNo);
        List<Room> roomList = roomMapper.selectList(roomLambdaQueryWrapper);
        if (roomList == null) {
            return buildingMapper.deleteById(buildingNo);
        } else {
            int deleteRoomFlag = 0;
            int deleteFlag = 0;
            for (Room room : roomList) {
                deleteRoomFlag = roomMapper.deleteById(room.getRoomNo());
                deleteFlag = deleteFlag + deleteRoomFlag;
            }
            int deleteBuildingFlag = buildingMapper.deleteById(buildingNo);
            return deleteBuildingFlag + deleteFlag;
        }
    }
}
